#!/bin/bash
set -eEuo pipefail
cd "$(dirname "$(readlink -f "$0")")"

source bash-buddy/lib/trap_error_info.sh
source bash-buddy/lib/common_utils.sh

################################################################################
# prepare
################################################################################

readonly default_build_jdk_version=11
# shellcheck disable=SC2034
PREPARE_JDKS_INSTALL_BY_SDKMAN=(
    8
    $default_build_jdk_version
    17
)

source bash-buddy/lib/prepare_jdks.sh

source bash-buddy/lib/java_build_utils.sh

# here use `install` and `-D performRelease` intended
#   to check release operations.
#
# De-activate a maven profile from command line
#   https://stackoverflow.com/questions/25201430
#
# shellcheck disable=SC2034
JVB_MVN_OPTS=(
    "${JVB_DEFAULT_MVN_OPTS[@]}"
    -DperformRelease
    -P'!gen-sign'
    # FIXME com.github.spotbugs:spotbugs-maven-plugin:check failed
    -P'!lint'
)

################################################################################
# ci build logic
################################################################################

cd ..

########################################
# default jdk 11, do build and test
########################################

export CI_TEST_MODE=true
export DCM_AGENT_SUPRESS_EXCEPTION_STACK=true

prepare_jdks::switch_to_jdk "$default_build_jdk_version"

cu::head_line_echo "build and test with Java: $JAVA_HOME"
jvb::mvn_cmd clean install

########################################
# test multi-version java
# shellcheck disable=SC2154
########################################
for jdk in "${PREPARE_JDKS_INSTALL_BY_SDKMAN[@]}"; do
    # already tested by above `mvn install`
    [ "$default_build_jdk_version" = "$jdk" ] && continue

    prepare_jdks::switch_to_jdk "$jdk"

    cu::head_line_echo "test with Java: $JAVA_HOME"
    # just test without build
    jvb::mvn_cmd surefire:test
done
